package com.secneo.appshare.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.secneo.appshare.R;
import com.secneo.appshare.api.LocalStore;
import com.secneo.appshare.api.RemoteApi.Feed;

public class FeedsAdapter extends BaseAdapter
{
	private List<Feed> mFeeds;
	private Context mContext;
	private boolean mStoped;
	private boolean mNewIconVisible = false;

	public FeedsAdapter(Context ctx, List<Feed> feeds)
	{
		this.mContext = ctx;
		this.mFeeds = feeds;
		this.mStoped = false;

	}

	public void freeDrawable()
	{
		mStoped = true;
		Log.d("MyTag", "Feeds bitmaps free !!! ");
		for (int i = 0; i < mFeeds.size(); i++)
		{
			if (!mFeeds.get(i).user_icon.contains("buildin") &&
					mFeeds.get(i).user_icon_drawable != null)
			{
				BitmapDrawable a = (BitmapDrawable) mFeeds.get(i).user_icon_drawable;
				if (a != null && !a.getBitmap().isRecycled())
					a.getBitmap().recycle();
				mFeeds.get(i).user_icon_drawable = null;
			}
		}
		System.gc();
	}

	@Override
	public int getCount()
	{
		return mFeeds.size();
	}

	@Override
	public Object getItem(int position)
	{
		return mFeeds.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	public void setNewIconVisible(boolean visible)
	{
		mNewIconVisible = visible;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		if (mStoped)
			return convertView;

		FeedItem feedItem;
		Feed feed = mFeeds.get(position);
		if (convertView == null)
		{
			View v = LayoutInflater.from(mContext).inflate(R.layout.appshare_user_item, null);

			feedItem = new FeedItem();
			feedItem.feedButtonLayout = (FrameLayout) v.findViewById(R.id.feedButtonLayout);
			feedItem.mAppIcon = (ImageView) v.findViewById(R.id.ivAppIcon);
			feedItem.weiboAppIcon = (ImageView) v.findViewById(R.id.weiboAppIcon);
			feedItem.huodongAppIcon = (ImageView) v.findViewById(R.id.huodongAppIcon);
			feedItem.mAppName = (TextView) v.findViewById(R.id.tvAppName);
			feedItem.newIcon = (ImageView) v.findViewById(R.id.is_new);

			v.setTag(feedItem);
			convertView = v;
		} else
		{
			feedItem = (FeedItem) convertView.getTag();
		}
		// set icon
		if (feed.user_icon_drawable == null)
		{
			feedItem.mAppIcon.setImageResource(R.drawable.some_user);
		} else
		{
			if (feed.snsType != 0)
			{
				int iconResource = LocalStore.getLocaleStoreInstance().getSmallIconResource(feed.snsType);
				if (iconResource != 0)
				{
					feedItem.weiboAppIcon.setImageResource(iconResource);
					feedItem.weiboAppIcon.setVisibility(View.VISIBLE);
				}
			} else
			{
				feedItem.weiboAppIcon.setVisibility(View.INVISIBLE);
			}

			// if new flower
			if (mNewIconVisible && feed.is_new)
			{
				feedItem.newIcon.setImageResource(R.drawable.new_flower);
				feedItem.newIcon.setVisibility(View.VISIBLE);
			} else
			{
				feedItem.newIcon.setVisibility(View.GONE);
			}

			feedItem.huodongAppIcon.setVisibility(View.VISIBLE);
			if (feed.type == 1)
			{
				feedItem.huodongAppIcon.setImageResource(R.drawable.shuzi1);
			} else if (feed.type == 2)
			{
				feedItem.huodongAppIcon.setImageResource(R.drawable.shuzi2);
			} else if (feed.type == 3)
			{
				feedItem.huodongAppIcon.setImageResource(R.drawable.shuzi3);
			} else if (feed.type == 4)
			{
				feedItem.huodongAppIcon.setImageResource(R.drawable.shuzi4);
			} else if (feed.type >= 5)
			{
				feedItem.huodongAppIcon.setImageResource(R.drawable.shuzi5);
			} else
				feedItem.huodongAppIcon.setVisibility(View.INVISIBLE);

			feedItem.mAppIcon.setImageDrawable(feed.user_icon_drawable);
		}

		// set name
		feedItem.mAppName.setText(feed.user_name);
		feedItem.feedButtonLayout.setVisibility(View.GONE);

		return convertView;
	}

	class FeedItem
	{
		FrameLayout feedButtonLayout;
		ImageView mAppIcon;
		ImageView weiboAppIcon;
		ImageView huodongAppIcon;
		ImageView newIcon;
		TextView mAppName;
	}
}
